<?php

/**
 * default actions.
 *
 * @package    bolao-aabb
 * @subpackage default
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class defaultActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->news_list = BolaoNewsPeer::getLastNews(sfConfig::get('app_max_news_on_homepage'));

    $this->month = $request->getParameter('month', date('m'));
    $this->year = $request->getParameter('year', date('Y'));
    $this->month_events = BolaoEventPeer::getMonthEvents($this->month, $this->year);

    $today = date('Y-m-d');
    $next_events = BolaoEventPeer::getNextEvents(sfConfig::get('app_max_events_on_homepage'));
    $this->events_by_day = BolaoEventPeer::organizeEventsByDay($next_events);
  }

  public function executeSendEmail(sfWebRequest $request)
  {
    $type = $request->getParameter('type');
    $id   = $request->getParameter('id');
    $error_messages = array();

    // Instanciate object
    switch($type)
    {
      case 'news':
        $object = BolaoNewsPeer::retrieveByPK($id);
        break;
      case 'event':
        $object = BolaoEventPeer::retrieveByPK($id);
        break;
      case 'album':
        $object = BolaoAlbumPeer::retrieveByPK($id);
        break;
      case 'tournament':
        $object = BolaoTournamentPeer::retrieveByPK($id);
        break;
      default:
        $this->redirect('@homepage');
    }

    // Mount return url based on object type
    switch($type)
    {
      case 'news':
        $returnUrl = $this->getController()->genUrl('news/index');
        break;
      case 'event':
        $returnUrl = $this->getController()->genUrl('event/index?month='.$object->getStartDate('m').'&year='.$object->getStartDate('Y'));
        break;
      case 'album':
        $returnUrl = $this->getController()->genUrl('album/show?id='.$object->getId());
        break;
      case 'tournament':
        $category_slug    = $object->getBolaoSubcategory()->getCategory()->getNameSlug();
        $subcategory_slug = $object->getBolaoSubcategory()->getNameSlug();
        $returnUrl = $this->getController()->genUrl('@show_tournament_by_name?year='.$object->getYear().'&name_slug='.$object->getNameSlug().'&category_slug='.$category_slug.'&subcategory_slug='.$subcategory_slug);
        break;
    }

    if ($request->isMethod('post'))
    {
      // Get e-mail content
      $list    = $request->getParameter('email_list');
      $summary = $request->getParameter('email_summary');
      $text    = $request->getParameter('email_text');

      // Send e-mails
      if (count($list) > 0)
      {
        foreach ($list as $email)
        {
          $message = $this->getMailer()->compose(
              array('info@gbaabb.com.br' => 'G B AABB'),
              $email,
              $summary
          );
          $message->setBody($text, 'text/html');
          $this->getMailer()->send($message);
        }

        // Redirect to destination
        $this->redirect($returnUrl);
      }
      else
        $error_messages[] = "&Eacute; necess&aacute;rio escolher um ou mais destinos para o e-mail.";
    }

    $this->objectType    = $request->getParameter('type');
    $this->object        = $object;
    $this->emailLists    = BolaoEmailListPeer::getAllLists();
    $this->returnUrl     = $returnUrl;
    $this->errorMessages = $error_messages;
  }

  public function executeTraining(sfWebRequest $request) {}

  public function executeContact(sfWebRequest $request) {}

  public function executeSitemap(sfWebRequest $request)
  {
    if (strcmp($request->getRequestFormat(), 'xml') == 0)
    {
      $request->setRequestFormat('html');
      $this->forward('sfSimpleGoogleSitemap', 'index');
    }
  }
}
